package 单周赛.history;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 *
 */
public class 第288场单周赛 {

    public static void main(String[] args) {

        System.out.println(largestInteger(30436));

//        System.out.println(minimizeResult("247+38"));

    }

    public static int largestInteger(int num) {
        int length = String.valueOf(num).length();
        int[] ji = new int[length];
        int[] ou = new int[length];

        Queue<Integer> jiQueue = new PriorityQueue<>((o1, o2) -> o2 - o1);
        Queue<Integer> ouQueue = new PriorityQueue<>((o1, o2) -> o2 - o1);

        int index = length - 1;
        while (num > 0) {
            int curNum = num % 10;
            num /= 10;
            if (curNum % 2 == 0) {
                if (curNum == 0) {
                    ou[index] = -1;
                } else {
                    ou[index] = curNum;
                }
                ouQueue.offer(curNum);
            } else {
                ji[index] = curNum;
                jiQueue.offer(curNum);
            }
            index--;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int curNum = 0;
            if (ou[i] != 0 || ou[i] == -1) {
                curNum = ouQueue.poll();
            } else if (ji[i] != 0) {
                curNum = jiQueue.poll();
            }
            stringBuilder.append(curNum);
        }

        return Integer.valueOf(stringBuilder.toString());
    }

//    public static String minimizeResult(String expression) {
//        String[] split = expression.split("\\+");
//
//        // 定义左括号放在index的左边
//        int leftMinIndex = 0, leftMinNum = Integer.valueOf(split[0]);
//        for (int i = 1; i < split[0].length(); i++) {
//            int curNum = Integer.valueOf(split[0].substring(0, i)) * Integer.valueOf(split[0].substring(i));
//            if (curNum < leftMinNum) {
//                leftMinIndex = i;
//                leftMinNum = curNum;
//            }
//        }
//        // 定义右括号放在index的右边
//        int rightMinIndex = split[1].length() - 1, rightMinNum = Integer.valueOf(split[1]);
//        for (int i = split[1].length() - 2; i >= 0; i--) {
//            int curNum = Integer.valueOf(split[1].substring(0, i == 0 ? 1 : i)) * Integer.valueOf(split[1].substring(i == 0 ? 1 : i));
//            if (curNum < rightMinNum) {
//                rightMinIndex = i;
//                rightMinNum = curNum;
//            }
//        }
//        int a = leftMinIndex == 0 ? 1 : Integer.valueOf(split[0].substring(0, leftMinIndex));
//        int b = Integer.valueOf(split[0].substring(leftMinIndex)) +
//                Integer.valueOf(split[1].substring(0, Math.min(split[1].length() - 1, rightMinIndex + 1)));
//        int c = rightMinIndex == split[1].length() - 1 ? 1 : Integer.valueOf(split[1].substring(rightMinIndex + 1));
//
//        int minNum = a * b * c;
//        StringBuilder stringBuilder = new StringBuilder();
//        if (minNum < Integer.valueOf(split[0]) + Integer.valueOf(split[1])) {
//            stringBuilder.append(split[0].substring(0, leftMinIndex)).append("(")
//                    .append(split[0].substring(leftMinIndex)).append("+")
//                    .append(split[1].substring(0, Math.min(split[1].length() - 1, rightMinIndex + 1)))
//                    .append(")").append(split[1].substring(rightMinIndex + 1));
//        } else {
//            stringBuilder.append("(").append(expression).append(")");
//        }
//        return stringBuilder.toString();
//    }

}
